package com.wica.view;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.wica.facetube.FaceTubeService;
import com.wica.facetube.R;
import com.wica.media.Player;
import com.wica.model.MovieClip;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class PlayVideoDialog extends Activity{
	private static final String LOG_TAG = "Rating";	
	private static final String[] RATINGS =  new String[] {"Poor","Poor","Nothing Special","Worth Watching","Pretty Cool","Awesome!"};

	protected FaceTubeService fts;
	
	private URL url;
	private InputStream is;
	private BitmapDrawable bmp;
	private ImageView image;	
	
	int percentageWatched = 100;
	String locationTxt = "At home";
	String location = "";
	String environment ="Alone";
	String URL="http://www.youtube.com/v/dMH0bHeiRNg";
	double totalrating = 0.00;
	private RatingBar totalRatingBar;
	private TextView totalRatingText,titleTextView,mRatingText;

	private String title;
	private Integer rating=0;
	private String mood;
	private float latitude;
	private float longitude;
	private float altitude;
	private Button skipRating;
	private String format1;
	private PlayVideoDialog context;
	private String URLThumbnail;
	protected String app_activity;
	protected String category_tags;
	protected String videoLength;
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // We inflate layout from rating.xml
        setContentView(R.layout.playdialog);   
        context = this;
        totalrating=new Double(this.getIntent().getExtras().get("totalRating").toString());
        title = this.getIntent().getExtras().get("title").toString();        
    	app_activity = this.getIntent().getExtras().get("app_activity").toString();
    	category_tags = this.getIntent().getExtras().get("category_tags").toString();
    	videoLength = this.getIntent().getExtras().get("videoLength").toString();

        titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText(title);
        Log.v("antes","antes");

        mRatingText = (TextView) findViewById(R.id.rating);

        Log.v("antes","antes");
        if (rating != 0)
        	mRatingText.setText(RATINGS[rating]);
        totalRatingText = (TextView) findViewById(R.id.totalrating);
        totalRatingText.setText("Users Rating: "+totalrating+"/5");

        // We copy the most recently changed rating on to these indicator-only
        // rating bars

        // The different rating bars in the layout. Assign the listener to us.
        totalRatingBar=(RatingBar)findViewById(R.id.small_totalratingbar);
        totalRatingBar.setRating((float)totalrating);
        skipRating = (Button) findViewById(R.id.btn_play);
    	URL=this.getIntent().getExtras().get("URL").toString();
    	format1=this.getIntent().getExtras().get("format1").toString();
        try {
			url = new URL(this.getIntent().getExtras().get("URLThumbnail").toString());
	        is = (InputStream) url.getContent();
		} catch (MalformedURLException e) {
			Log.e(LOG_TAG,e.toString());
		} catch (IOException e) {
			Log.e(LOG_TAG,e.toString());
		}
        bmp = (BitmapDrawable) BitmapDrawable.createFromStream(is , "youtube");
        image = (ImageView) findViewById(R.id.imageview);
        image.setImageDrawable(bmp);
        
       	Intent intent = new Intent(PlayVideoDialog.this, Player.class);
        intent.putExtra("rating", rating);
    	setResult(RESULT_OK,intent);
        
    	URLThumbnail= this.getIntent().getExtras().get("URLThumbnail").toString();
        fts = new FaceTubeService();
        skipRating.setOnClickListener(
                new OnClickListener() {

					public void onClick(View v) {
                    	Intent i = (new Intent(context, Player.class)).setData(Uri.parse(format1));
                    	i.putExtra("URL", URL);    
                    	i.putExtra("URLThumbnail", URLThumbnail);    	
                    	i.putExtra("totalRating", totalrating);
                    	i.putExtra("title", title);
            			i.putExtra("category_tags",category_tags);	    	
            	    	i.putExtra("app_activity", app_activity);
            	    	i.putExtra("videoLength", videoLength);

                    	context.finish();
                    	context.startActivity(i);   
                    	
                    }
                }
            ); 

    }//onCreate method
	
	/** By overriding onConfigurationChanged method we avoid recreation when orientation changes.
	 *  Check Android manifest for more details
	 *  
	 *  {@inheritDoc}
	 **/
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
	}//onConfigurationChanged method

}//PlayVideoDialog class